$(document).ready(function() {
	$("#s-form").submit(function(){
		var email = $("input[name=email]").val();
		var text = $("#form-text").val();
		var errors = 0;
		if(checkEmail(email) == 0){
			$("#email-error").text('Wprowadź poprawny e-mail');
			$("input[name=email]").addClass('ferror');
			errors = 1;
		}else{
			$("input[name=email]").removeClass('ferror');
			$("#email-error").text('');
			errors = 0;
		}
		if(text == ''){
			$("#text-error").text('Pole nie może być puste');
			$("#form-text").addClass('ferror');
			errors = 1;
		}else{
			$("#form-text").removeClass('ferror');
			$("#text-error").text('');
			errors = 0;
		}
		if(errors == 1) return false;
		var form_str = $(this).serialize();
		$.ajax({
		type: "POST",
			url: "index.php/sendcontactform",
			data: form_str,
			beforeSend: function(){
			var loading = '<div style="text-align:center;"><img src="media/img/loading.gif" /></div>';
			$('#snippet-form').html(loading);
		},
			error: function(xhr, desc, e) {
			   alert(xhr.responseText);
			},
			success: function(msg){
				$('#snippet-form').html('Wiadomość została wysłana');
				return false;
			}
		})
		return false;
	})
})
function checkEmail(email) {
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
    if(email.search(emailRegEx) == -1) {
		return 0;
    }
}
